﻿using AutoMapper;
using Common.Helper;
using DbModel;
using DbModel.Models;
using DbModel.ViewModel.UserInfo;
using IServices.Base;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly IBaseServices<UserInfo> _userInfoService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfoService"></param>
        /// <param name="mapper"></param>
        public AuthController(IBaseServices<UserInfo> userInfoService, IMapper mapper)
        {
            _userInfoService = userInfoService;
            _mapper = mapper;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<string>> Login(string loginName, string password)
        {
            var jwtStr = string.Empty;
            if (string.IsNullOrEmpty(loginName) || string.IsNullOrEmpty(password))
            {
                return new MessageModel<string>()
                {
                    Success = false,
                    Msg = "用户名或密码不能为空"
                };
            }

            var pass = MD5Helper.MD5Decrypt32(password);
            var userInfo = await _userInfoService.FindAsync(x => x.LoginName == loginName && x.LoginPassWord == pass);
            if (userInfo == null)
            {
                return new MessageModel<string>()
                {
                    Success = false,
                    Msg = "认证失败"
                };
            }

            jwtStr = GetUserJwt(userInfo);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "获取成功",
                Response = jwtStr
            };
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="createUserInfoInputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Register(CreateUserInfoInputDto createUserInfoInputDto)
        {
            var userInfo = await _userInfoService.FindAsync(x => x.LoginName == createUserInfoInputDto.LoginName);
            if (userInfo != null)
            {
                return new MessageModel<string>
                {
                    Success = false,
                    Msg = "账户已存在"
                };
            }
            userInfo = await _userInfoService.FindAsync(x => x.Email == createUserInfoInputDto.Email);
            if (userInfo != null)
            {
                return new MessageModel<string>
                {
                    Success = false,
                    Msg = "邮箱已存在"
                };
            }

            userInfo = await _userInfoService.FindAsync(x => x.Phone == createUserInfoInputDto.Phone);
            if (userInfo != null)
            {
                return new MessageModel<string>
                {
                    Success = false,
                    Msg = "手机号码已注册"
                };
            }

            userInfo = await _userInfoService.FindAsync(x => x.UserName == createUserInfoInputDto.UserName);
            if (userInfo != null)
            {
                return new MessageModel<string>
                {
                    Success = false,
                    Msg = "用户名已存在"
                };
            }

            createUserInfoInputDto.LoginPassWord = MD5Helper.MD5Decrypt32(createUserInfoInputDto.LoginPassWord);
            var user = _mapper.Map<UserInfo>(createUserInfoInputDto);
            user.CreateTime = DateTime.Now;
            await _userInfoService.InsertAsync(user, true);
            var jwtStr = GetUserJwt(user);

            return new MessageModel<string>
            {
                Success = false,
                Msg = "注册成功",
                Response = jwtStr
            };
        }

        private string GetUserJwt(UserInfo userInfo)
        {
            var tokenModel = new TokenModelJwt() { Uid = userInfo.Id, Role = "user" };
            return JwtHelper.IssueJwt(tokenModel);
        }
    }
}
